package com.example.s.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.s.Activity.team.CreateTeamActivity;
import com.example.s.Activity.team.MyTeamActivity;
import com.example.s.Activity.team.TeamDetailActivity;
import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.example.s.bean.TeamItem;
import com.example.s.utils.LiveDataBus;
import com.example.s.view.MyNav;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class TeamFragment extends BaseFragment {
    private CircleImageView headPhoto;
    private EditText searchContent;
    private ImageView addTeam;
    private TabLayout teamNavTop;
    private TextView city;
    private ViewPager teamVp;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<List<TeamItem>> lists = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_team;
    }

    @Override
    public void initView(View view) {

        headPhoto = (CircleImageView) findViewById(R.id.headPhoto);
        searchContent = (EditText) findViewById(R.id.searchContent);
        addTeam = (ImageView) findViewById(R.id.addTeam);
        teamNavTop = (TabLayout) findViewById(R.id.teamNavTop);
        teamVp = (ViewPager) findViewById(R.id.teamVp);

        city = (TextView) findViewById(R.id.city);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setData();


        List<MyNav.NavItem> navItems = new ArrayList<>();
        navItems.add(new MyNav.NavItem(0, "学习"));
        navItems.add(new MyNav.NavItem(0, "运动"));
        navItems.add(new MyNav.NavItem(0, "游玩"));
        navItems.add(new MyNav.NavItem(0, "更多"));
        new MyNav<List<TeamItem>, TeamItem>(teamNavTop, teamVp, navItems, new MyNav.OnMyNavListener() {
            @Override
            public void onCommonListener(ImageView icon, TextView title) {
                title.setTextColor(Color.parseColor("#4DFFFFFF"));
                title.setTextSize(14);
            }

            @Override
            public void onSelectListener(ImageView icon, TextView title, int pos) {
                title.setTextColor(Color.parseColor("#D9408A"));
                title.setTextSize(16);
            }
        })
                .setVpRecyclerData(new LinearLayoutManager(getContext()), lists, R.layout.rv_team_item)
                .setRecyclerCallBack(new MyNav.OnRecyclerCallBack<TeamItem>() {
                    @Override
                    public void handleRecyclerData(RecyclerView view, MyRecyclerViewAdapter<TeamItem> adapter, InnerHolder holder, TeamItem item) {
                        holder.setSrcResource(R.id.headPhoto, item.getFakePic())
                                .setSrcResource(R.id.cover, item.getFakeConver());
                        holder.setText(R.id.userName, item.getmUserName());
                        holder.setText(R.id.title, item.getmTitle());
                        holder.setText(R.id.title, item.getmTitle());
                        holder.setText(R.id.goOffPlace, item.getmGoOffPlace());
                        holder.setText(R.id.goOffTime, item.getmGoOffTime());
                        holder.setText(R.id.destination, item.getmDestination());
                        holder.setText(R.id.releaseTime, item.getmRealseTime());
                        holder.setText(R.id.teamMember, item.getmTeamMemberNum());
                        holder.setOnClickListener(R.id.teamItem, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LiveDataBus.getInstance().with("currentTeam", TeamItem.class).setValue(item);
                                startActivity(new Intent(getActivity(), TeamDetailActivity.class));
                            }
                        });
                    }
                });


        addTeam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), CreateTeamActivity.class));
            }
        });

        headPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyTeamActivity.class));
            }
        });
    }


    private int getX() {
        int x = (int) (Math.random() * 31);
        return x;
    }

    private int getY() {
        int x = (int) ((Math.random() * 20) + 8);
        return x;
    }

    private int getZ() {
        int x = (int) ((Math.random() * 8) + 3);
        return x;
    }

    private int getZ1() {
        int x = (int) ((Math.random() * 2) + 1);
        return x;
    }

    private void setData() {
        //"大花","顾天云","灰灰","曙光","等一个你","xuanni","触景生情","听涛听你","沉默","从未离去","bangi","kkkww"
        //"一起跑步","去图书馆看书","乒乓球","野炊聚餐","约一波羽毛球","聚餐分享美食","去图书馆学习","打排球","跑步健身","打篮球","王者荣耀上分"
        ArrayList<TeamItem> teamItems = new ArrayList<TeamItem>();                 //"学习"
        ArrayList<TeamItem> teamItems2 = new ArrayList<TeamItem>();                //"运动"
        ArrayList<TeamItem> teamItems3 = new ArrayList<TeamItem>();                //"游玩"
        ArrayList<TeamItem> teamItems4 = new ArrayList<TeamItem>();                //"更多"
        teamItems.add(new TeamItem(R.drawable.yywhd4, R.drawable.yywtx23, "", "顾天云", "", "去图书馆看书", "2020-12-" + getX() + " " + getY() + ":00", "西三门口", "图书馆一楼", getZ(), "2020-1-" + getZ1()));
        teamItems.add(new TeamItem(R.drawable.yywhd4, R.drawable.yywtx7, "", "触景生情", "", "去图书馆学习", "2020-12-" + getX() + " " + getY() + ":00", "西一门口", "川有图书馆一楼", getZ(), "2020-1-" + getZ1()));

        teamItems2.add(new TeamItem(R.drawable.yywhd1, R.drawable.yywtx10, "", "大花", "", "一起跑步", "2020-12-" + getX() + " " + getY() + ":00", "西一门口", "操场", getZ(), "2021-1-" + getZ1()));
        teamItems2.add(new TeamItem(R.drawable.yywhd5, R.drawable.yywtx31, "", "灰灰", "", "乒乓球", "2020-12-" + getX() + " " + getY() + ":00", "西一门口", "大礼堂", getZ(), "2020-1-" + getZ1()));
        teamItems2.add(new TeamItem(R.drawable.yywhd7, R.drawable.yywtx5, "", "等一个你", "", "约一波羽毛球", "2020-12-" + getX() + " " + getY() + ":00", "大礼堂", "大礼堂", getZ(), "2020-1-" + getZ1()));
        teamItems2.add(new TeamItem(R.drawable.yywhd10, R.drawable.yywtx8, "", "听涛听你", "", "打排球", "2020-12-" + getX() + " " + getY() + ":00", "大礼堂", "前门操场", getZ(), "2020-1-" + getZ1()));
        teamItems2.add(new TeamItem(R.drawable.yywhd2, R.drawable.yywtx10, "", "从未离去", "", "打篮球", "2020-12-" + getX() + " " + getY() + ":00", "篮球场", "前门篮球场", getZ(), "2020-1-" + getZ1()));
        teamItems2.add(new TeamItem(R.drawable.yywhd2, R.drawable.yywtx11, "", "bangi", "", "打篮球", "2020-12-" + getX() + " " + getY() + ":00", "篮球场", "前门篮球场", getZ(), "2020-1-" + getZ1()));
        teamItems2.add(new TeamItem(R.drawable.yywhd1, R.drawable.yywtx9, "", "沉默", "", "跑步健身", "2020-12-" + getX() + " " + getY() + ":00", "西一门口", "操场", getZ(), "2020-1-" + getZ1()));


        teamItems3.add(new TeamItem(R.drawable.yywhd6, R.drawable.yywtx4, "", "曙光", "", "野炊聚餐", "2020-12-" + getX() + " " + getY() + ":00", "学校后门", "龙泉山", getZ(), "2020-1-" + getZ1()));
        teamItems3.add(new TeamItem(R.drawable.yywhd6, R.drawable.yywtx6, "", "xuanni", "", "聚餐分享美食", "2020-12-" + getX() + " " + getY() + ":00", "学校后门", "太平山", getZ(), "2020-1-" + getZ1()));

        teamItems4.add(new TeamItem(R.drawable.yywhd6, R.drawable.yywtx6, "", "xuanni", "", "聚餐分享美食", "2020-12-" + getX() + " " + getY() + ":00", "学校后门", "太平山", getZ(), "2020-1-" + getZ1()));

        lists.add(teamItems);
        lists.add(teamItems2);
        lists.add(teamItems3);
        lists.add(teamItems4);
    }


}
